#include "CustomButton.h"


CustomButton::CustomButton()
{
    m_bIsSelected = false;
	m_TouchByArea = false;

	m_NormalImage = NULL;
	m_SelectedImage = NULL;
}
bool CustomButton::initWithNormalImage(const char* _NormalImage, const char* _SlectedImage)
{
	m_bIsSelected = false;

	m_NormalImage = CCSprite::spriteWithFile(_NormalImage);
	m_SelectedImage = CCSprite::spriteWithFile(_SlectedImage);

	m_SelectedImage->setIsVisible( false );

	this->addChild( m_NormalImage );
	this->addChild( m_SelectedImage );

	ComputeBoundingTouch();

	return true;
}
CustomButton* CustomButton::ButtonWithNormalImage(const char* _NormalImage, const char* _SlectedImage)
{
	CustomButton *pRet = new CustomButton();
	if (pRet && pRet->initWithNormalImage(_NormalImage, _SlectedImage) )
	{
		pRet->autorelease();
		return pRet;
	}
	CC_SAFE_DELETE(pRet);
	return NULL;
}

void CustomButton::SetisSelected(bool _b) 
{
	m_bIsSelected = _b;

	if( m_bIsSelected )
	{
		m_NormalImage->setIsVisible( false );
		m_SelectedImage->setIsVisible( true );
	}
	else
	{
		m_NormalImage->setIsVisible( true );
		m_SelectedImage->setIsVisible( false );
	}
}

void CustomButton::ComputeBoundingTouch()
{
	CCRect rect = m_NormalImage->boundingBox();
	rect.origin = convertToWorldSpace( rect.origin );
	rect.size.width = rect.size.width * getScaleX();
	rect.size.height = rect.size.height * getScaleY();

	m_BoundingTouch = rect;
}

void CustomButton::drawBoundingBox()
{
	//ComputeBoundingTouch();

	ccDrawSquare( m_BoundingTouch.origin , CCPoint(m_BoundingTouch.origin.x + m_BoundingTouch.size.width, m_BoundingTouch.origin.y + m_BoundingTouch.size.height ) );
}